/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.asportoonline.tags;

import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import org.asportoonline.entities.Locale;

/**
 *
 * @author fe
 */
public class LocaliCercatiTag extends SimpleTagSupport {
    private List<Locale> locali;

    public List<Locale> getLocali() {
        return locali;
    }

    public void setLocali(List<Locale> locali) {
        this.locali = locali;
    }
    
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        List<Locale> ll = getLocali();
        Locale l;
        String link;
        String indirizzo;
        String[] chiusura = {"Nessuno", "Luned&igrave;", "Marted&igrave;", "Mercoled&igrave;", "Gioved&igrave;", "Venerd&igrave;", "Sabato", "Domenica"};
        int colspan;
        
        try {
            
            if(ll==null || ll.isEmpty()) {     
                out.print("<p>Nessun locale trovato!!</p>");
            }
            else {
                out.print("<table border=\"1\">");
                
                for(int i=0; i<ll.size(); i++) {
                    l = ll.get(i);
                    link = "MainController?q=fetch_restaurant_details_action&ret=restaurant_details&idlocale="+l.getIdLocale();
                    indirizzo = l.getIndirizzo().getVia() + ", " + l.getIndirizzo().getCitta();
                    
                    if(l.getSecondaApertura()!=null && !l.getSecondaApertura().isEmpty()) {
                        colspan = 8;
                    }
                    else {
                        colspan = 7;
                    }
                    
                    out.print("<tr>"
                            + "<th colspan=\"2\"><a href=\""+link+"\">" + l.getNome() + "</a></th>"
                            + "<td rowspan=\""+colspan+"\"><img src=\"" + l.getUrlimmagine() + "\" alt=\"restaurant image\" /></td>"
                            + "</tr>"
                            + "<tr><td>Indirizzo:</td><td>" + indirizzo + "</td></tr>"
                            + "<tr><td>Email:</td><td>" + l.getEmail() + "</td></tr>"
                            + "<tr><td>Prima apertura:</td><td>" + l.getPrimaApertura() + "</td></tr>");
                    
                    if(l.getSecondaApertura()!=null && !l.getSecondaApertura().isEmpty()) {
                        out.print("<tr><td>Seconda apertura:</td><td>" + l.getSecondaApertura() + "</td></tr>");
                    }
                    
                    out.print("<tr><td>Distanza massima:</td><td>" + l.getDistanzaMassima() + " km</td></tr>"
                            + "<tr><td>Giorno di chiusura:</td><td>" + chiusura[l.getGiornoChiusura()+1] + "</td></tr>"
                            + "<tr><td>Telefono:</td><td>" + l.getTelefono() + "</td></tr>");                                        
                }
                
                out.print("</table>");
                out.flush();                
            }
        } catch (java.io.IOException ex) {
            throw new JspException("Error in LocaliTag tag", ex);
        }
    }
}
